package com.hq.fxh.No0_ChengQu.Activity;

/**
 * Created by admin on 2017/8/3.
 */

import android.Manifest;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hq.fxh.No0_ChengQu.Login.LoginActivity;
import com.hq.fxh.No1_Infromation.ShowLocation;
import com.hq.fxh.No2_BBS.Fragment.Fragment_BBS;
import com.hq.fxh.No1_Infromation.Infor.Fragment.InformationFragment;
import com.hq.fxh.No0_ChengQu.Adapter.PAdapter_Home;
import com.hq.fxh.No3_TuiGuang.Fragment.Fragment_TuiGuang;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No4_XiaoXi.Fragment_XiaoXi_KuangJia;
import com.hq.fxh.No5_User.User.Fragment.Fragment_User;
import com.hq.fxh.R;
import com.hq.fxh.Receiver.MyService;
import com.hq.fxh.CustomerView.NoScrollViewPager;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.PermissionsActivity;
import com.hq.fxh.Utils.PermissionsChecker;
import com.hq.fxh.Utils.ScreenManager;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


import cn.jiguang.analytics.android.api.JAnalyticsInterface;


/**
 * Created by admin on 2017/8/3.
 */

public class HomeActivity extends BaseActivity {

    public NoScrollViewPager mViewPager;
    Fragment_TuiGuang mFragment3;
    Fragment_XiaoXi_KuangJia mFragment4;
    Fragment_User mFragment5;
    Fragment_BBS mFragment2;
    InformationFragment mFragment1;
    ArrayList<Fragment> fragments;
    public LinearLayout home;
    PAdapter_Home mPagerAdapter;

    private TabLayout mTabLayout;

    private ArrayList<String> titles = new ArrayList<>();

    private boolean isSelected = false;

    private int[] tabIcons = {
            R.drawable.inforiconchange,
            R.drawable.bbsiconchange,
            R.drawable.tuiguangiconchange,
            R.drawable.messageiconchange,
            R.drawable.meiconchange,
    };
    public LocationManager myLocationManager;

    @Override
    protected int getLayoutId() {
        return R.layout.custom_tabitem_layout;
    }

    private void initListenter() {

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabStatus(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabStatus(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    @Override
    protected void initView() {
        mViewPager = (NoScrollViewPager) findViewById(R.id.view_pager);
        mTabLayout = (TabLayout) findViewById(R.id.toolbar_tab);
        home = (LinearLayout) findViewById(R.id.home);


    }

    @Override
    protected void initData() {
        Intent myServiceIntent = new Intent(this, MyService.class);
        startService(myServiceIntent);
        mPermissionsChecker = new PermissionsChecker(this);
        getpermission();
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                fragments.get(position).onResume();
                if (position == 2) {
                    if (SharedPreferenceUtils.getMemberId(HomeActivity.this) == 0) {
                        startActivity(new Intent(HomeActivity.this, LoginActivity.class));
                    } else {
                        int isCompany = SharedPreferenceUtils.getIsCompany(HomeActivity.this);
                        if (isCompany == 0) {//个人版


                        } else if (isCompany == 1) {//企业版


                        }
                        setSysColor(R.color.red, true);

                    }
                } else if (position == 4) {
                    if (SharedPreferenceUtils.getMemberId(HomeActivity.this) == 0) {
                        setSysColor(R.color.red, true);

                    } else {
                        setSysColor(R.color.red, false);

                    }

                } else {
                    setSysColor(R.color.red, false);

                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        getData();
        initListenter();
    }


    private void changeTabStatus(TabLayout.Tab tab, boolean selected) {
        View view = tab.getCustomView();
        ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        imgTitle.setVisibility(View.VISIBLE);
        if (selected) {
            txtTitle.setTextColor(getResources().getColor(R.color.textred));
        } else {
            txtTitle.setTextColor(getResources().getColor(R.color.textblack));
        }
    }

    public View getTabView(final int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        final ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        txtTitle.setText(titles.get(position));
        imgTitle.setImageResource(tabIcons[position]);
        if (position == 0) {
            txtTitle.setTextColor(getResources().getColor(R.color.textred));
        } else {
            txtTitle.setTextColor(getResources().getColor(R.color.textblack));
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mViewPager.setCurrentItem(position);
                imgTitle.setSelected(true);

            }


        });
        return view;
    }

    @Override
    protected void onStart() {
        super.onStart();
        JAnalyticsInterface.onPageStart(this, "首页面");
    }

    @Override
    protected void onStop() {
        super.onStop();
        JAnalyticsInterface.onPageEnd(this, "首页面");

    }

    public void getData() {
        mFragment5 = new Fragment_User();
        mFragment4 = new Fragment_XiaoXi_KuangJia();
        mFragment3 = new Fragment_TuiGuang();
        mFragment2 = new Fragment_BBS();
        mFragment1 = new InformationFragment();
        if (fragments == null) {
            fragments = new ArrayList<>();
        }
        fragments.add(mFragment1);
        fragments.add(mFragment2);
        fragments.add(mFragment3);
        fragments.add(mFragment4);
        fragments.add(mFragment5);

        titles.add("资讯");
        titles.add("论坛");
        titles.add("推广");
        titles.add("消息");
        titles.add("我的");
        mViewPager.setOffscreenPageLimit(5);

        mPagerAdapter = new PAdapter_Home(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.getTabAt(0).setCustomView(getTabView(0));
        mTabLayout.getTabAt(1).setCustomView(getTabView(1));
        mTabLayout.getTabAt(2).setCustomView(getTabView(2));
        mTabLayout.getTabAt(3).setCustomView(getTabView(3));
        mTabLayout.getTabAt(4).setCustomView(getTabView(4));


    }


    private List<Address> getAddress(Location location) {
        List<Address> result = null;
        try {
            if (location != null) {
                Geocoder gc = new Geocoder(this, Locale.getDefault());
                result = gc.getFromLocation(location.getLatitude(),
                        location.getLongitude(), 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private boolean gpsIsOpen() {
        boolean isOpen = true;
        if (!myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {//没有开启GPS
            isOpen = false;
        }
        return isOpen;
    }

    private boolean netWorkIsOpen() {
        boolean netIsOpen = true;
        if (!myLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//没有开启网络定位
            netIsOpen = false;
        }
        return netIsOpen;
    }

    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                ToastUtils.show(HomeActivity.this, "再按一次退出程序");
                ShowLocation showLocation = new ShowLocation();
                showLocation.getLocationMessage(this);
                mExitTime = System.currentTimeMillis();
            } else {

                ScreenManager.getInstance().AppExit(this);
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    private PermissionsChecker mPermissionsChecker; // 权限

    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION

    };

    public void getpermission() {
        if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
            startPermissionsActivity();
        } else {
            LogUtils.e(" 权限获取成功");
        }
    }

    // 权限检查
    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(this, 0, PERMISSIONS);
    }

}
